{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Yolo-v2 DW2TF validation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "\n",
    "# Random input data to test \n",
    "in_data = np.random.randn(64, 608, 608, 3) * 120.56"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Output from Darkflow converted Yolo-v2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from /data/darkflow/yolov2.ckpt\n"
     ]
    }
   ],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "tf.train.import_meta_graph('/data/darkflow/yolov2.ckpt.meta')\n",
    "ckpt_path = '/data/darkflow/yolov2.ckpt'\n",
    "\n",
    "g = tf.get_default_graph()\n",
    "in_t = g.get_tensor_by_name('input:0')\n",
    "out_t = g.get_tensor_by_name('output:0')\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    var_list = {} # dictionary mapping variable name to tensor, used to create saver object to restore\n",
    "    reader = tf.train.NewCheckpointReader(ckpt_path)\n",
    "    for key in reader.get_variable_to_shape_map():\n",
    "        # Look for all variables in ckpt that are used by the graph\n",
    "        try:\n",
    "            tensor = g.get_tensor_by_name(key + \":0\")\n",
    "        except KeyError:\n",
    "            # This tensor doesn't exist in the graph (for example it's\n",
    "            # 'global_step' or a similar housekeeping element) so skip it.\n",
    "            continue\n",
    "        var_list[key] = tensor\n",
    "    saver = tf.train.Saver(var_list=var_list)\n",
    "    saver.restore(sess, ckpt_path)\n",
    "\n",
    "    out_data_darkflow = sess.run(out_t, feed_dict={in_t: in_data})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Output from DW2TF converted Yolo-v2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from /data/DW2TF/data/yolov2.ckpt\n"
     ]
    }
   ],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "tf.train.import_meta_graph('/data/DW2TF/data/yolov2.ckpt.meta')\n",
    "ckpt_path = '/data/DW2TF/data/yolov2.ckpt'\n",
    "\n",
    "g = tf.get_default_graph()\n",
    "in_t = g.get_tensor_by_name('yolov2/net1:0')\n",
    "out_t = g.get_tensor_by_name('yolov2/convolutional23/BiasAdd:0')\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    var_list = {} # dictionary mapping variable name to tensor, used to create saver object to restore\n",
    "    reader = tf.train.NewCheckpointReader(ckpt_path)\n",
    "    for key in reader.get_variable_to_shape_map():\n",
    "        # Look for all variables in ckpt that are used by the graph\n",
    "        try:\n",
    "            tensor = g.get_tensor_by_name(key + \":0\")\n",
    "        except KeyError:\n",
    "            # This tensor doesn't exist in the graph (for example it's\n",
    "            # 'global_step' or a similar housekeeping element) so skip it.\n",
    "            continue\n",
    "        var_list[key] = tensor\n",
    "    saver = tf.train.Saver(var_list=var_list)\n",
    "    saver.restore(sess, ckpt_path)\n",
    "    \n",
    "    out_data_dw2tf = sess.run(out_t, feed_dict={in_t: in_data})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### RMS Error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(64, 19, 19, 425)\n",
      "(64, 19, 19, 425)\n",
      "1.0358586146179594e-14\n"
     ]
    }
   ],
   "source": [
    "print(out_data_darkflow.shape)\n",
    "print(out_data_dw2tf.shape)\n",
    "print(np.mean(out_data_darkflow - out_data_dw2tf)**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ -3.7852187    5.77546     -4.7733483   -3.2303553  -21.29596\n",
      "  16.798992    17.598959    13.808735    -0.97558796 -14.386962  ]\n"
     ]
    }
   ],
   "source": [
    "print(out_data_darkflow[0,0,0,:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ -3.7852492    5.7753325   -4.7733445   -3.2302966  -21.295998\n",
      "  16.798998    17.598883    13.8088255   -0.97567666 -14.386923  ]\n"
     ]
    }
   ],
   "source": [
    "print(out_data_dw2tf[0,0,0,:10])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Reorg layer test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 4, 1)\n",
      "(1, 1, 2, 4)\n"
     ]
    }
   ],
   "source": [
    "batch_size, height, width, channels = 1, 2, 4, 1\n",
    "stride = 2\n",
    "\n",
    "_height, _width, _channel = height // stride, width // stride, channels * stride * stride\n",
    "\n",
    "input = np.array([[[[0], [2], [1], [3]], [[4], [6], [5], [7]]]])\n",
    "\n",
    "net = input\n",
    "net = tf.reshape(net, [batch_size, _height, stride, _width, stride, channels])\n",
    "net = tf.transpose(net, [0, 1, 3, 2, 4, 5])  # batch_size, _height, _width, stride, stride, channels\n",
    "net = tf.reshape(net, [batch_size, _height, _width, stride * stride * channels], name='reorg')\n",
    "\n",
    "output = net.eval(session=tf.Session())\n",
    "\n",
    "print(input.shape)\n",
    "print(output.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[0],\n",
       "         [2],\n",
       "         [1],\n",
       "         [3]],\n",
       "\n",
       "        [[4],\n",
       "         [6],\n",
       "         [5],\n",
       "         [7]]]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[0, 2, 4, 6],\n",
       "         [1, 3, 5, 7]]]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert input[0,0,0,0] == output[0,0,0,0]\n",
    "assert input[0,0,1,0] == output[0,0,0,1]\n",
    "assert input[0,0,2,0] == output[0,0,1,0]\n",
    "assert input[0,0,3,0] == output[0,0,1,1]\n",
    "assert input[0,1,0,0] == output[0,0,0,2]\n",
    "assert input[0,1,1,0] == output[0,0,0,3]\n",
    "assert input[0,1,2,0] == output[0,0,1,2]\n",
    "assert input[0,1,3,0] == output[0,0,1,3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Read darknet .weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "weight_array = np.fromfile('/data/darknet/yolov2.weights', dtype='f')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50983565,)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weight_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
